package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.CategoryMapper;
import com.dmpp.mes.dao.TypeMapper;
import com.dmpp.mes.parameterInfo.Category;
import com.dmpp.mes.parameterInfo.Type;
import com.dmpp.mes.service.CategoryService;
import com.dmpp.mes.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "TypeService")

public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper mapper;

    @Override
    public ResponseData findAll(Type type) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(type));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(String code) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(code));
        return responseData;
    }

    @Override
    public ResponseData insert(Type type) {
        ResponseData responseData = new ResponseData();
        Type type1= mapper.findByCode(type.getCode());
        if (type1==null){
            mapper.insert(type);
        }else {
            responseData.setCodeEnum(RespEnum.ERROR2001);
        }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(Type type) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(type);
        return responseData;
    }

    @Override
    public ResponseData deleteByCode(String code) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(code);
        return responseData;
    }

    @Override
    public ResponseData delete(String code) {
        ResponseData responseData = new ResponseData();
        Integer number = mapper.count(code);
        if (number ==0){
            mapper.delete(code);
        }else {
            responseData.setCodeEnum(RespEnum.ERROR1016);
        }
        return responseData;
    }

}
